//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;

namespace OpenLS.Core.Internals
{
    public class CompositeUndoUnit : ICompositeUndoUnit
    {
        private readonly string text;
        private Stack<IUndoUnit> children;
        private ICompositeUndoUnit lastUnit;
        private ICompositeUndoUnit openedUnit;
        private object parent;

        public CompositeUndoUnit(string text)
        {
            this.text = text;
        }


        internal UndoManager UndoManager
        {
            get
            {
                object p = Parent;
                while (p is ICompositeUndoUnit)
                    p = ((ICompositeUndoUnit) p).Parent;
                return (UndoManager) p;
            }
        }

        public Stack<IUndoUnit> Children
        {
            get
            {
                if (children == null)
                    children = new Stack<IUndoUnit>();
                return children;
            }
            set { children = value; }
        }

        public ICompositeUndoUnit LastUnit
        {
            get { return lastUnit; }
            set { lastUnit = value; }
        }

        protected ICompositeUndoUnit DeepestOpenUnit
        {
            get
            {
                ICompositeUndoUnit result = OpenedUnit;
                if (result != null)
                {
                    while (result.OpenedUnit != null)
                    {
                        result = result.OpenedUnit;
                    }
                }
                return result;
            }
        }

        #region ICompositeUndoUnit Members

        public string Text
        {
            get { return text; }
        }

        public bool IsEmpty
        {
            get { return children == null || Children.Count == 0; }
        }

        public void Add(IUndoUnit unit)
        {
            Children.Push(unit);
        }

        public virtual void Do()
        {
            ICompositeUndoUnit u = GetEmptyClone();
            UndoManager m = UndoManager;
            if (m != null && m.IsEnabled)
                m.Open(u);
            if (Children.Count == 0)
                Debug.WriteLine("Warning empty children");
            DoSelf();
            if (m != null && m.IsEnabled)
                m.Close(u);
        }


        public void Close()
        {
            Close(OpenedUnit);
        }

        public ICompositeUndoUnit OpenedUnit
        {
            get { return openedUnit; }
            set { openedUnit = value; }
        }


        public object Parent
        {
            get { return parent; }
            set { parent = value; }
        }


        public virtual void Open(ICompositeUndoUnit newUnit)
        {
            if (newUnit == null)
                throw new ArgumentNullException("newUnit");
            ICompositeUndoUnit deepestOpenUnit = DeepestOpenUnit;
            if (deepestOpenUnit == null)
            {
                openedUnit = newUnit;
                newUnit.Parent = this;
                
            }
            else
            {
                newUnit.Parent = deepestOpenUnit;
                deepestOpenUnit.Open(newUnit);
            }
        }


        public virtual bool Merge(IUndoUnit unit)
        {
            return false;
        }

        #endregion

        protected virtual void DoSelf()
        {
            while (children != null && Children.Count > 0)
            {
                IUndoUnit u = Children.Pop();
                u.Do();
            }
        }


        internal virtual ICompositeUndoUnit GetEmptyClone()
        {
            return new CompositeUndoUnit(Text);
        }

        private void Close(IUndoUnit unit)
        {
            Contract.Requires(unit == OpenedUnit);
            if (OpenedUnit.OpenedUnit != null)
            {
                OpenedUnit.OpenedUnit.Close();
            }
            ICompositeUndoUnit openUnit = OpenedUnit;
            OpenedUnit = null;
            Add(openUnit);
            LastUnit = openUnit;
        }
    }
}